package studio.raptor.dispatcher.server.handler.consumption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import studio.raptor.databus.command.EventMetaData;
import studio.raptor.dispatcher.server.context.HandlerContext;
import studio.raptor.dispatcher.server.handler.Handler;
import studio.raptor.dispatcher.server.service.FilterService;

/**
 * 对从消息队列上获取的消息进行过滤，
 * 根据订阅关系判断消息是否需要被发送.
 *
 * @author jack
 * @since 1.0
 */
@Component
public class FilterHandler extends Handler {

  private static final Logger log = LoggerFactory.getLogger(FilterHandler.class);

  @Autowired
  FilterService filterService;

  @Override
  public boolean process(HandlerContext context) {

    EventMetaData eventMetaData = context.getEvent().getEventMetaData();
    boolean result = filterService.filterEvent(context.getCode(), eventMetaData);
    log.debug("Consumer[{}] {} subscribe to event [{}]", context.getCode(),
        result ? "do" : "do not",
        eventMetaData.getBusinessNo());
    if (result) {
      result = getSuccessor().process(context);
    }
    return result;
  }


}
